<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['diy']=$this->plugin_setting('diy');
$basic=$this->plugin_setting('basic');
//日期 今天，未来七天
$data=array();
$days=$basic['days']>0?$basic['days']:7;
$yue0=0;
if($basic['no_yueday']){
		$days++;
		$yue0=1;
}
for($i=$yue0;$i<$days;$i++){
		$data[]=date("Y-m-d",strtotime("+".$i." day"));
}
if($_RQ['op']=='main'){	
	$serve=pdo_get('jy_qiuyuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['serve_id']),array('open_total','id','title','price','serve_time','package','serve_types','week6_addfee','week7_addfee','week6_addtime','week7_addtime'));
	$worker=pdo_get('jy_qiuyuyue_store_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['worker_id']),array('name','headimg','sex','age','years'));
	$return['day']=$data[$_RQ['dayid']];
	$onetime=pdo_get('jy_qiuyuyue_serve_times',array('onday'=>$return['day'],'serveid'=>$_RQ['serve_id'],'uniacid'=>$_SESSION['uniacid']));
	if($onetime['serve_time']){
		$serve['serve_time']=$onetime['serve_time'];
	}
	$serve['serve_time']=json_decode($serve['serve_time'],true);
	$weekid=date("w",strtotime($return['day']));
	$weekid=$weekid?:7;
	$timeids=str_array($_RQ['timeids']);
	$serve_times=array();
	$totalprice=0;
	$yetime=mktime(18,00,0,date('m'),date('d'),date('Y'));
	$day=date('Y-m-d',TIMESTAMP);
	$total=count(str_array($_RQ['users']));
	$total++;
	foreach($timeids as $tv){
		$timekey=substr($tv, 0, -1);
		$fee=$serve['serve_time'][intval($timekey)]['fee']?:$serve['price'];
		$nowtime=$serve['serve_time'][intval($timekey)];
		$starttime=strtotime($day.' '.$nowtime['times']['start']);
		if($starttime>$yetime){
			$isye=1;
		}
		if($weekid==6 && $serve['week6_addfee']>0){
			if($isye && $serve['week6_addtime']!=1){
				$fee +=$serve['week6_addfee'];
			}
			if(!$isye && $serve['week6_addtime']!=2){
				$fee +=$serve['week6_addfee'];
			}
			
		}
		if($weekid==7 && $serve['week7_addfee']>0){
			if($isye && $serve['week7_addtime']!=1){
				$fee +=$serve['week7_addfee'];
			}
			if(!$isye && $serve['week7_addtime']!=2){
				$fee +=$serve['week7_addfee'];
			}
		}
		if($_RQ['is_ban']){
			$fee=50*$fee/100;
		}
		$fee=$fee*$total;
		$serve_times[]=array(
			'times'=>$serve['serve_time'][intval($timekey)]['times'],
			'fee'=>$fee,
			'id'=>$tv,
		);
		$totalprice +=$fee;
	}
	if($serve['serve_types']){
		$newserve_types=array();
		if(strpos($serve['serve_types'],';0;')!== false){ 
			$newserve_types[0]=array('id'=>0,'title'=>'到店服务');
			$serve_type='0';
		}
		if(strpos($serve['serve_types'],';1;')!== false){ 
			$newserve_types[1]=array('id'=>1,'title'=>'上门服务');
			$serve_type=1;
		}
		$serve['serve_types']=$newserve_types;
	}else{
		$serve['serve_types']=array(
			array('id'=>'0','title'=>'到店服务'),
			array('id'=>'1','title'=>'上门服务')
		);
	}
	$return['serve']=$serve;
	$member1=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('nickname','mobile'));	
	$member = pdo_get('jy_qiuyuyue_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$member['myuser']=$member['myuser']?json_decode($member['myuser'],true):array();
	$return['member']=array_merge($member1,$member);
	$return['worker']=$worker;
	$return['serve_times']=$serve_times;
	//次卡信息：
	$timesorder=pdo_get('jy_qiuyuyue_timeserve_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'lasttimes >'=>0,'serve_id'=>$serve['id']),array('id','lasttimes','goods_id'));
	$timeserve=pdo_get('jy_qiuyuyue_timeserve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$timesorder['goods_id']),array('title'));
	$return['timesorder']=$timesorder;
	$return['timeserve']=$timeserve;
	if($timesorder['id']){
		$totalprice=0.00;
	}
	$return['plate']=pdo_get('jy_qiuyuyue_serve_plate',array('id'=>intval($_RQ['plate_id']),'uniacid'=>$_SESSION['uniacid']));
	$return['post']=array(
		'timesorderid'=>intval($timesorder['id']),
		'serve_id'=>intval($_RQ['serve_id']),
		'is_ban'=>intval($_RQ['is_ban']),
		'timeids'=>$_RQ['timeids'],
		'users'=>$_RQ['users'],
		'total'=>$total,
		'plate_id'=>intval($_RQ['plate_id']),
		'store_id'=>intval($_RQ['store_id']),
		'dayid'=>intval($_RQ['dayid']),
		'price'=>$totalprice,
		'totalprice'=>$totalprice,
		'username'=>$return['member']['nickname'],
		'mobile'=>$return['member']['mobile'],
		'serve_type'=>$serve_type,
		'serve_time'=>$serve_times,
	);
	$return['store']=pdo_get('jy_qiuyuyue_store',array('id'=>$return['post']['store_id']));
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$serve_id = $this->session('serve_id');
	$is_ban = $postdata['is_ban'];
	if($postdata){
		if(!$postdata['username']){
			exi('请输入联系人姓名','error');
		}
		if(!$postdata['mobile']){
			exi('请输入联系人电话','error');
		}
		if(!preg_match("/^1[345678]{1}\d{9}$/",$postdata['mobile'])){  	 
			exi('请输入正确的手机号码','error');
		} 
		if($postdata['fileds']){
			foreach ($postdata['fileds'] as $item) {
				if($item['ismust']==1 && empty($item['data'])){
					exi('请完善'.$item['title'].'内容后再提交','error');
				}
				if($item['type']==7){
					$item['data']=$item['data']['province'].$item['data']['city'].$item['data']['district'].$item['data']['xiangxi'];	
				}
			}
		}
		$timeids=str_array($postdata['timeids']);
		$serve_times=array();
		$totalprice=0;
		$serve=pdo_get('jy_qiuyuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$postdata['serve_id']),array('open_total','id','title','price','serve_time','package','serve_types','week6_addfee','week7_addfee','week6_addtime','week7_addtime'));
		$onetime=pdo_get('jy_qiuyuyue_serve_times',array('onday'=>$data[$postdata['dayid']],'serveid'=>$postdata['serve_id'],'uniacid'=>$_SESSION['uniacid']));
		if($onetime['serve_time']){
			$serve['serve_time']=$onetime['serve_time'];
		}
		$serve['serve_time']=json_decode($serve['serve_time'],true);
		foreach($timeids as $tv){
			//已支付，已确认，已完成订单数
			$timekey=substr($tv, 0, -1);
			$total=$serve['serve_time'][intval($timekey)]['total'];
			$res=$this->model('order')->checktimeid($is_ban,$tv,$postdata['store_id'],$data[$postdata['dayid']],$postdata['serve_id'],$postdata['plate_id'],$serve['open_total'],$total);
			if(!$res){
				exi($tv.'该场次已被预约了！','error');
			}elseif($postdata['total']>$res){
				exi($tv.'该场次只剩'.$res.'人了！','error');
			}
		}
		$users=str_array($postdata['users']);
		$member = pdo_get('jy_qiuyuyue_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		$member['myuser']=$member['myuser']?json_decode($member['myuser'],true):array();
		foreach($users as &$uv){
			$uv=$member['myuser'][$uv];
		}
		$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'plate_id'=>$postdata['plate_id'],
				'store_id'=>$postdata['store_id'],
				'serve_id'=>$postdata['serve_id'],
				'timeid'=>$postdata['timeids'],
				'serve_time'=>ijson_encode($postdata['serve_time']),
				'day'=>$data[$postdata['dayid']],
				'address'=>$postdata['address'],
				'username'=>$postdata['username'],
				'mobile'=>$postdata['mobile'],
				'remark'=>$postdata['remark'],
				'ordersn'=>get_ordersn('jy_qiuyuyue_server_order','ordersn'),
				'fileds'=>ijson_encode($postdata['fileds']),
				'users'=>ijson_encode($users),
				'money'=>$postdata['price'],
				'pay'=>$postdata['totalprice'],
				'createtime'=>TIMESTAMP,
				'timesorderid'=>intval($postdata['timesorderid']),
				'is_ban'=>$is_ban,
				'total'=>$postdata['total']?:1,
		);
		pdo_insert('jy_qiuyuyue_server_order',$insert);
		$id=pdo_insertid();
		$params=array(
				'id'=>$id,
				'title'=>'预约场地订单',
				'fee'=>$postdata['totalprice'],
				'plugin'=>'jy_qiuyuyue',
				//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
				'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
				'back_url'=>'jy_qiuyuyue/book/payresult',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
				'success_url'=>'jy_qiuyuyue/server_order/',
				'error_url'=>'jy_qiuyuyue/server_order/',
		);
		$payid=cfc('pay')->create_pay_order($params);
		$this->pdo_update('server_order',array('pay_id'=>$payid),array('id'=>$id));
		$res=array(
				'pay_id'=>$payid,
				'order_id'=>$id,
		);
		exi($res);
	}
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_qiuyuyue_server_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['pay']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_qiuyuyue_server_order',array('status'=>'1','transid'=>$payresult['transaction_id'],'paytype'=>$payresult['pay_type']),array('id'=>$order['id']));
	//计次减免
	if($order['timesorderid']){
		pdo_update('jy_qiuyuyue_timeserve_order',array('lasttimes -='=>1),array('id'=>$order['timesorderid']));
	}
	if($this->plugin_exists('jy_commission')){
		//分销计入
		$order['price']=$order['pay'];
		$order['plugin_name']='jy_qiuyuyue';
		model('jy_commission/commission')->create($order);
	}
	$this->model('notice')->notice_yuyueorder($order);
	exi('支付成功','success','jy_qiuyuyue/server_order/list/status=1&id='.$order['id']);
}